//
//  HeaderInfoViewController.m
//  Quick_News
//
//  Created by 曾凯峻 on 15/1/30.
//  Copyright (c) 2015年 zkj. All rights reserved.
//

#import "HeaderInfoViewController.h"
#import "TopNewsInfo.h"
#import "HeaderCountViewController.h"
@interface HeaderInfoViewController ()

@end

@implementation HeaderInfoViewController
//内存管理
-(void)dealloc
{
    [self.topNews release];
    [self.newsInfo release];
    [self.progress release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    if (!self.newsInfo) {
        self.progress = [[MBProgressHUD alloc] initWithView:self.view];
        self.progress.labelText = @"请稍后";
        [self.progress show:YES];
        [self.view addSubview:self.progress];
        [self.progress release];
        [self creatData];
    }
    else {
        [self creatView];
    }
    self.navigationController.navigationBar.translucent = NO;
}
- (void)creatData
{
    NSString *str = [NSString stringWithFormat:@"http://c.3g.163.com/nc/article/%@/full.html", self.topNews.docid];
    //NSLog(@"%@", str);
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *tempDic = [responseObject objectForKey:self.topNews.docid];
        self.newsInfo = [[TopNewsInfo alloc] init];
        [self.newsInfo setValuesForKeysWithDictionary:tempDic];
        [self.progress hide:YES];
        [self creatView];
        // NSLog(@"%@", self.newsInfo.body);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self.progress hide:YES];
        
        NSString *str = [NSString stringWithFormat:@"http://c.3g.163.com/nc/article/%@/full.html", self.topNews.docid];
      //  NSLog(@"%@", str);
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
        [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSDictionary *tempDic = [responseObject objectForKey:self.topNews.docid];
            self.newsInfo = [[TopNewsInfo alloc] init];
            [self.newsInfo setValuesForKeysWithDictionary:tempDic];
            [self creatView];
            // NSLog(@"%@", self.newsInfo.body);
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            //警告
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"对不起" message:@"网络加载出错啦" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alertView show];
            [alertView release];
            NSLog(@"HeaderInfoError:%@", error);
        }];
    }];
    
}
//处理字符串里字符
- (void)FixBody
{
    self.newsInfo.body = [self.newsInfo.body stringByReplacingOccurrencesOfString:@"</p>" withString:@"                                                                                                    "];
    self.newsInfo.body = [self.newsInfo.body stringByReplacingOccurrencesOfString:@"<p>" withString:@""];
    self.newsInfo.body = [self.newsInfo.body stringByReplacingOccurrencesOfString:@"<b>" withString:@""];
    self.newsInfo.body = [self.newsInfo.body stringByReplacingOccurrencesOfString:@"</b>" withString:@""];
    self.newsInfo.body = [self.newsInfo.body stringByReplacingOccurrencesOfString:@"<br/>" withString:@""];
    self.newsInfo.body = [self.newsInfo.body stringByReplacingOccurrencesOfString:@"<strong>" withString:@""];
    self.newsInfo.body = [self.newsInfo.body stringByReplacingOccurrencesOfString:@"</strong>" withString:@""];
    self.newsInfo.body = [self.newsInfo.body stringByReplacingOccurrencesOfString:@"<!--VIDEO#0-->" withString:@""];
    self.newsInfo.body = [self.newsInfo.body stringByReplacingOccurrencesOfString:@"<!--VIDEO#1-->" withString:@""];
    self.newsInfo.body = [self.newsInfo.body stringByReplacingOccurrencesOfString:@"<!--VIDEO#2-->" withString:@""];
    self.newsInfo.body = [self.newsInfo.body stringByReplacingOccurrencesOfString:@"<!--SPINFO#0-->" withString:@""];
    self.newsInfo.body = [self.newsInfo.body stringByReplacingOccurrencesOfString:@"<!--@@PRE-->" withString:@""];
    self.newsInfo.body = [self.newsInfo.body stringByReplacingOccurrencesOfString:@"<!--@@H2-->" withString:@""];
    self.newsInfo.body = [self.newsInfo.body stringByReplacingOccurrencesOfString:@"<!--@@H1-->" withString:@""];
    for (NSInteger i = 0; i < 50; i++) {
        self.newsInfo.body = [self.newsInfo.body stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"<!--link%ld-->", i] withString:@""];
    }
    for (NSInteger i = 0; i < 50; i++) {
        self.newsInfo.body = [self.newsInfo.body stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"<!--IMG#%ld-->", i] withString:@""];
    }
}


- (void) creatView
{
    [self FixBody];
    
    //右上角导航栏
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:[NSString stringWithFormat:@"评论:%ld", self.newsInfo.replyCount] style:UIBarButtonItemStylePlain target:self action:@selector(rightBarAction:)] autorelease];
    
    
   //  NSLog(@"%@", self.newsInfo.img);
    
    //创建可以滑动scrollView
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 10, self.view.bounds.size.width - 20, self.view.bounds.size.height - 20)];
  //  scrollView.backgroundColor = [UIColor greenColor];
    scrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:scrollView];
    [scrollView release];
    
    //图片
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 100, scrollView.frame.size.width - 30, 170)];
    [imageView setImageWithURL:[NSURL URLWithString:self.topNews.imgsrc]];
    imageView.userInteractionEnabled = YES;
   // imageView.backgroundColor = [UIColor orangeColor];
    [scrollView addSubview:imageView];
    [imageView release];
    

    
    
    
    //标题
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 290, 40)];
    titleLabel.font = [UIFont boldSystemFontOfSize:21.0f];
    titleLabel.text = self.newsInfo.title;
 //    titleLabel.backgroundColor = [UIColor redColor];
    [scrollView addSubview:titleLabel];
    [titleLabel release];
    //来源
    UILabel *scoreLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 40, 200, 30)];
    scoreLabel.text = [NSString stringWithFormat:@"来源:  %@", self.newsInfo.source];
    scoreLabel.font = [UIFont boldSystemFontOfSize:15.0f];
  //  scoreLabel.backgroundColor = [UIColor redColor];
    [scrollView addSubview:scoreLabel];
    [scoreLabel release];
    //时间
    UILabel *ptimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 65, 200, 30)];
    ptimeLabel.text = [NSString stringWithFormat:@"时间:  %@", self.newsInfo.ptime];
    ptimeLabel.font = [UIFont boldSystemFontOfSize:15.0f];
   // ptimeLabel.backgroundColor = [UIColor redColor];
    [scrollView addSubview:ptimeLabel];
    [ptimeLabel release];
    
    
 
    
    
    
    
    //内容
    UILabel *bodyLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 280, scrollView.frame.size.width, 300)];
            bodyLable.text = self.newsInfo.body;
            bodyLable.numberOfLines = 0;
            [bodyLable sizeToFit];
            
    //偏移量以信息为准
    scrollView.contentSize =  CGSizeMake(0, bodyLable.bounds.size.height + 290);
            [scrollView addSubview:bodyLable];
            [bodyLable release];
    
}
//评论按钮
- (void)rightBarAction:(UIButton *)bar
{
    HeaderCountViewController *headerCount = [[HeaderCountViewController alloc] init];
    headerCount.CountDocid = self.newsInfo.docid;
    headerCount.board = self.newsInfo.replyBoard;
    headerCount.count = self.newsInfo.replyCount;
    [self.navigationController pushViewController:headerCount animated:YES];
    [headerCount release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
